local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill {
  name = "lb__xinshou",
  tags = { Skill.Limited }
}

Fk:loadTranslationTable {
  ["lb__xinshou"] = "心狩",
  [":lb__xinshou"] = "<b>限定技</b>，当你指定或成为【杀】的目标时，若你的体力值为1，使用者可以将之改为【决斗】，然后对方将体力值和手牌数调整至与你相同，且本回合视为拥有<a href=':lb__sanwu'><font color='red'>〖三无〗</font></a>。",

  ["#lb__xinshou_filter"] = "心狩",
  ["#lb__xinshou_view"] = "心狩：你可以如手牌般使用装备区中的非装备牌",
  ["#lb__xinshou-from"] = "心狩：你可以将 %arg 改为【决斗】，然后 %src 将体力值和手牌数调整至与你相同，且本回合视为拥有〖三无〗",
  ["#lb__xinshou-to"] = "心狩：你可以将 %arg 改为【决斗】，然后将体力值和手牌数调整至与 %src 相同，且本回合视为拥有〖三无〗",
  ["@@lb__xinshou"] = "心狩",

  ["$lb__xinshou1"] = "大捷，已定！",
  ["$lb__xinshou2"] = "翾翔不坠，万载常胜！",
}

local xinshou_spec = {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and data.card.trueName == "slash"
        and player.hp == 1 and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local from = player
    local prompt = "#lb__xinshou-from:" .. data.to.id .. "::" .. data.card:toLogString()
    if event == fk.TargetConfirming then
      from = data.from
      prompt = "#lb__xinshou-to:" .. player.id .. "::" .. data.card:toLogString()
    end
    return player.room:askToSkillInvoke(from, { skill_name = skel.name, prompt = prompt })
  end,
  ---@param player ServerPlayer
  on_use = function(self, event, target, player, data)
    data:changeCard("duel")
    local room = player.room
    local change = data.to
    if event == fk.TargetConfirming then
      change = data.from
    end
    LB_U.BalanceHP(change, player.hp, skel.name)
    if change:isAlive() then
      LB_U.BalanceCards(change, skel.name, player:getHandcardNum())
    end
    if change:isAlive() and not change:hasSkill("lb__sanwu", true) then
      room:handleAddLoseSkills(change, "lb__sanwu")
      local Turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if Turn then
        Turn:addCleaner(function()
          if change:hasSkill("lb__sanwu", true) then
            room:handleAddLoseSkills(change, "-lb__sanwu")
          end
        end)
      end
    end
  end,
}

skel:addEffect(fk.TargetSpecifying, xinshou_spec)
skel:addEffect(fk.TargetConfirming, xinshou_spec)


return skel
